package com.soecode.lyf.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.soecode.lyf.dto.Result;
import com.soecode.lyf.entity.Collect;
import com.soecode.lyf.service.CollectService;

@Controller
@RequestMapping("/collect") // url:/模块/资源/{id}/细分 /seckill/list
public class CollectController {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private CollectService collectservice;
	
	@ResponseBody
	@RequestMapping(value="/collectionlist", method = RequestMethod.GET)
	public Result collectionList(String userid,Model model) {
		Result result = new Result();
		if(!StringUtils.isEmpty(userid)) {
		List<Collect> list = collectservice.selectCollection(userid);
		model.addAttribute("CollectLIST",list);
		result =new Result(0,model,"查询成功");
		}
		return result;
	}
	
	@ResponseBody
	@RequestMapping(value="/collection", method = RequestMethod.GET)
	public Result addCollection(String imgpath, String imgtext, String imghref, String router, String userid,String jcread) {
		Result result = new Result();
		if(!StringUtils.isEmpty(imgpath)&&!StringUtils.isEmpty(imgtext)&&!StringUtils.isEmpty(imghref)&&!StringUtils.isEmpty(router)&&!StringUtils.isEmpty(userid)&&!StringUtils.isEmpty(jcread)) {
			Date time = new Date();
	        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String thistime = df.format(time);
			int i = collectservice.addCollect(imgpath,imgtext,imghref,router,userid,thistime,jcread);
			if(i==0) {
			result = new Result(1,"收藏失败");	
			}else {
			result = new Result(0,"收藏成功");
			}
		}
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/quitcollect", method = RequestMethod.GET)
	public Result quitCollection(String jcname, String userid) {
		Result result = new Result();
		if(!StringUtils.isEmpty(jcname)&&!StringUtils.isEmpty(userid)) {
			int i = collectservice.deleteCollection(userid, jcname);
			if(i==0) { // 删除失败
				result = new Result(1,"删除收藏课程失败");
			}else { // 删除成功
				result = new Result(0,"删除收藏课程成功");
			}
		}
		return result;
	}
	public Collect isExist(String imgtext,String userid) { // 判断用户是否在数据库中存在
		Collect col = collectservice.SelectCollect(imgtext, userid);
		return col;
	}
}
